package com.yanqu.road.server.cmd.unionwar;

import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarDetail;
import com.yanqu.road.entity.enums.eUnionWarWinMark;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.CrossUnionWarProto;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = CrossProtocol.C_UNION_WAR_PREPARE_BATTLE_DATA_STATISTICS, desc = "战斗数据统计")
public class CrossUnionBattleDataStatisticsReqCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        UnionWarActivityServerProto.UnionBattleDataStatisticsReqMsg reqMsg = UnionWarActivityServerProto.UnionBattleDataStatisticsReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        int phase = reqMsg.getPhase();
        int session = reqMsg.getSession();

        //统计返回
        UnionWarActivityServerProto.UnionBattleDataStatisticsRespMsg.Builder clientMsg = UnionWarActivityServerProto.UnionBattleDataStatisticsRespMsg.newBuilder();
        clientMsg.setActivityId(activityId);
        clientMsg.setRet(0);
        clientMsg.setPhase(phase);
        clientMsg.setSession(session);

        long userId = packet.getPlayerId();
        //拿商会
        String unionUid = Cross2UnionActivityGroupMgr.getUnionUid(activityId, userId);
        if(StringUtils.isNullOrEmpty(unionUid)){
            return;
        }

        //拿备战
        CrossUnionWarDetail detail = CrossUnionWarMgr.getCrossUnionWarDetailReadOnly(activityId, phase, session, unionUid);
        if(detail != null){
            if(detail.getWinMark() == eUnionWarWinMark.Win.getValue() || detail.getWinMark() == eUnionWarWinMark.Lose.getValue()) {
                //返回战斗明细
                CrossUnionWarProto.CrossUnionWarUnionBattleTempMsg battleTempMsg = CrossUnionWarMgr.parseBattleResultBuilder(activityId, phase, session, detail).build();
                if (battleTempMsg != null) {
                    clientMsg.setRight(battleTempMsg);
                    if (!StringUtils.isNullOrEmpty(battleTempMsg.getEnemyUnionUid())) {
                        CrossUnionWarDetail enemyDetail = CrossUnionWarMgr.getCrossUnionWarDetail(activityId,  phase, session, detail.getEnemyUnionUid());
                        CrossUnionWarProto.CrossUnionWarUnionBattleTempMsg enemyBattleTempMsg = CrossUnionWarMgr.parseBattleResultBuilder(activityId, phase, session, enemyDetail).build();
                        if (enemyBattleTempMsg != null) {
                            clientMsg.setLeft(enemyBattleTempMsg);
                            clientMsg.setRet(0);
                        }
                    }
                } else {
                    clientMsg.setRet(GameErrorCode.E_UNION_WAR_ACTIVITY_BATTLE_DETAIL_NO_INTEGRITY);
                }
            }
            MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(Protocol.U_UNION_WAR_PREPARE_BATTLE_DATA_STATISTICS, clientMsg));
        }
    }

    @Override
    public void sendData() {

    }
}
